package org.backend.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.backend.pojo.entity.Address;
import org.backend.service.AddressService;
import org.backend.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.backend.utils.JwtUtil.getOpenId;
import static org.backend.utils.JwtUtil.setOpenId;

@RestController
@RequestMapping("/address")
@Slf4j
public class AddressController {

    @Autowired
    AddressService addressService;

    /**
     * 新增地址
     *
     * @param address
     * @param request
     * @return
     */
    @PostMapping("/add")
    public ApiResponse<String> add(@RequestBody Address address, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        address = setOpenId(address, token);
        log.info("add : {}", address);
        String message = addressService.add(address);
        if (message == null) {
            return ApiResponse.error("信息不能为空");
        }
        return ApiResponse.success(message);
    }

    /**
     * 根据openid和userid查找所有地址信息
     *
     * @param userId
     * @param request
     * @return
     */
    @PostMapping({"/getAddress", "/getAddress/{userId}"})
    public ApiResponse<List<Address>> getAddress(@PathVariable(required = false) Integer userId, HttpServletRequest request) {
        String openid = getOpenId(request.getHeader("Authorization"));
        if (userId == null && openid != null) {
            userId = -1;
            List<Address> addressList = addressService.getAddress(openid, userId);
            return ApiResponse.success(addressList);
        }
        // todo 网页版暂无
        return ApiResponse.error("网页版暂无");
    }

    /**
     * 根据对应的地址id查找地址信息
     *
     * @param id
     * @return
     */
    @PostMapping("/getAddressById/{id}")
    public ApiResponse<Address> getAddressById(@PathVariable Integer id) {
        Address address = addressService.getAddressById(id);
        if (address == null) {
            throw new IllegalArgumentException("查找地址失败");
        }
        return ApiResponse.success(address);
    }

    /**
     * 根据地址id设置默认地址
     *
     * @param id
     * @return
     */
    @PostMapping("/setDefaultAddress/{id}")
    public ApiResponse<String> setDefaultAddress(@PathVariable Integer id) {
        log.info(id.toString());
        return addressService.setDefaultAddress(id);
    }

    /**
     * 获取默认地址
     * @param userId
     * @param request
     * @return
     */
    @PostMapping({"/getDefaultAddress", "/getDefaultAddress/{userId}"})
    public ApiResponse<Address> getDefaultAddress(@PathVariable(required = false) Integer userId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String openid = getOpenId(token);
        if (userId == null && openid != null) {
            userId = -1;
            Address address = addressService.getDefaultAddress(openid, userId);
            if (address == null) {
                throw new IllegalArgumentException("获取默认地址失败");
            }
            return ApiResponse.success(address);
        }
        // todo 网页版暂无
        return ApiResponse.error("网页版暂无");
    }

}
